package com.citrix.gs.util;

import java.io.File;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.citrix.gs.util.base.PerforceUtil;
import com.citrix.gs.util.common.Config;

public class RepoConfig extends PerforceUtil  {
    
    private static Logger logger = LoggerFactory.getLogger(RepoConfig.class);
    private static RepoConfig instance = null;
    private String path;
    private Document doc;
    
    private RepoConfig() throws DocumentException {
        super();
        initConfigServer();
        
        this.path = Config.me().getRepoConfigPerforcePath();
        this.doc = getXmlAsDoc(path);
        
        if(this.doc == null) {
            this.path = Config.me().getRepoConfigLocalPath();
            SAXReader reader = new SAXReader();
            this.doc = reader.read(new File(path));
        }
    }
    
    public static RepoConfig me(){
        if(instance == null) {
            try {
                instance = new RepoConfig();
            } catch (Exception e) {
                logger.error("Construct error: ", e);
            }
        }
        
        return instance;
    }
    
    public String getOwer(String repoName){
        Element el = getOwerEl(repoName);
        if(el == null) return "";
        
        return String.format("%s(%s)", el.attributeValue("name"), el.getText());
    }
            
    private Element getOwerEl(String repoName) {
        
        String [] repoNameArr = repoName.split("-");
        
        if(repoNameArr.length != 3) {
            logger.error("Repository name {} is not correct", repoName);
        }
        
        String product = repoNameArr[0];
        String component = repoNameArr[1];
        String branch = repoNameArr[2];
        
        logger.info("Product: {}, Component: {}, Branch: {}", product, component, branch);
        
        Node branchNode = doc.selectSingleNode(String.format("/products/product[@name='%s']/component[@name='%s']/branch[@name='%s']", product, component, branch));
        
        Element parent = null;
        if(branchNode != null) { 
            parent = branchNode.getParent();
            
        }else {
            logger.error("The repository {} is not found in repository config file, try to find parent component", repoName);
            parent = (Element)doc.selectSingleNode(String.format("/products/product[@name='%s']/component[@name='%s']", product, component));
        }
        
        if(parent != null) {
            do {
                Node ownerNode = parent.selectSingleNode("owners/owner");
                
                if(ownerNode != null) {
                    return (Element)ownerNode;
                }
                parent = parent.getParent();
            } while (parent != null);
        } else {
            logger.error("The repository {} and parent component are not found in repository config file ", repoName);
        }
        
        return null;
    }

}
